package hk.ust.android_competition.pet;

import hk.ust.android_competition.store.Costume;
import hk.ust.android_competition.store.CostumeType;
import hk.ust.android_competition.store.Food;
import hk.ust.android_competition.store.Shower;

public class Pet implements PetBehavior {
    public final int maxHappiness = 100;
    public final int maxHealth = 100;
    public final int maxSleepy = 100;
    public final int maxClean = 100;
    private final int minHappiness = 0;
    private final int minHealth = 0;
    private final int minSleepy = 0;
    private final int minClean = 0;
    private final int happinessLevel = 40;
    private final int healthyLevel = 50;
    private final int sleepyLevel = 80;
    private final int cleanLevel = 30;
    private final int valueOfHappinessChangedPerTime = -1;
    private final int valueOfHealthChangedPerTime = -2;
    private final int valueOfSleepyChangedPerTime = 2;
    private final int valueOfCleanChangedPerTime = -3;
    private String petName;
    private PetType type;
    private double happiness;
    private double health;
    private double sleepy;
    private double clean;
    private Costume costume = new Costume("Default", 0, 0, CostumeType.None);

    public Pet(String name, int type) {

        petName = name;
        switch (type) {
            case 0:
                this.type = PetType.Panda;
                break;
            case 1:
                this.type = PetType.Rabbit;
                break;
            case 2:
                this.type = PetType.Giraffe;
                break;
        }
        happiness = 100;
        health = 100;
        sleepy = 0;
        clean = 100;
    }

    public Costume getCostume() {
        return costume;
    }

    public void wearCostume(Costume costume) {
        this.costume = costume;
    }

    public void reset() {
        //happiness = 100;
        happiness = maxHappiness + costume.getMaxStatusIncrease();
        //health = 100;
        health = maxHealth + costume.getMaxStatusIncrease();
        sleepy = 0;
        //clean = 100;
        clean = maxClean + costume.getMaxStatusIncrease();
    }

    public PetType getType() {
        return type;
    }

    public Pet(String name, int happiness, int health, int sleepy, int clean) {
        this.petName = name;
        this.happiness = happiness;
        this.clean = clean;
        this.health = health;
        this.sleepy = sleepy;
    }

    public void timePass(double second) {
        happiness += second * valueOfHappinessChangedPerTime;
        health += second * valueOfHealthChangedPerTime;
        sleepy += second * valueOfSleepyChangedPerTime;
        clean += second * valueOfCleanChangedPerTime;
        update();
    }

    private void update() {
        if (happiness < minHappiness)
            happiness = minHappiness;
        else if (happiness > maxHappiness + costume.getMaxStatusIncrease())
            happiness = maxHappiness +  + costume.getMaxStatusIncrease();
        if (health < minHealth)
            health = minHealth;
        else if (health > maxHealth + costume.getMaxStatusIncrease())
            health = maxHealth + costume.getMaxStatusIncrease();
        if (sleepy < minSleepy)
            sleepy = minSleepy;
        else if (sleepy > maxSleepy + costume.getMaxStatusIncrease())
            sleepy = maxSleepy + costume.getMaxStatusIncrease();
        if (clean < minClean)
            clean = minClean;
        else if (clean > maxClean + costume.getMaxStatusIncrease())
            clean = maxClean + costume.getMaxStatusIncrease();
    }

    public int getHappiness() {
        return (int) happiness;
    }

    public int getHealth() {
        return (int) health;
    }

    public int getSleepy() {
        return (int) sleepy;
    }

    public int getClean() {
        return (int) clean;
    }

    public String getPetName() {
        return petName;
    }

    public void speak(String sentence) {
        happiness++;
    }

    public void eat(Food food) {
        health += food.getHungerIncrease();
        happiness += food.getHappinessIncrease();
        health += food.getHealthIncrease();
    }

    public void play() {
        happiness += 10;
        update();
    }

    public String showStatus() {
        String response = "I am ";
        if (isSleepy()) {
            response += "sleepy, ";
        }
        if (!isHealthy()) {
            response += "sick, ";
        } else {
            response += "healthy, ";
        }
        if (!isClean()) {
            response += "and dirty.";
        } else {
            response += "and clean.";
        }
        return response;
    }

    public void listenASong() {
        this.sleepy = 0;
    }

    public boolean isSleepy() {
        if (sleepy >= sleepyLevel)
            return true;
        else
            return false;
    }

    public boolean isHealthy() {
        if (health >= healthyLevel)
            return true;
        else
            return false;
    }

    public boolean isHappy() {
        if (happiness >= happinessLevel)
            return true;
        else
            return false;
    }

    public boolean isClean() {
        if (clean >= cleanLevel)
            return true;
        else
            return false;
    }

    public void shower(Shower shower) {
        clean = health;
        update();
    }

    public void smile() {
        happiness += 2;
        update();
        ;
    }
}
